home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / ColorUtils.java < prev    next >
Text File  |  1998-08-21  |  7KB  |  223 lines

  1. package symantec.itools.awt.util;
  2.  
  3. import java.awt.Color;
  4. import java.util.Hashtable;
  5. import java.lang.IllegalArgumentException;
  6.  
  7. //     07/08/97    LAB    Removed checkValidPercent() function, and replaced
  8. //                    calls to it with calls to symantec.itools.util.GeneralUtils.checkValidPercent().
  9. //     08/05/97    LAB    Added lightness, calculateHilightColor, and calculateShadowColor.
  10. //                    Updated version to 1.1.  Removed GetColor, and corresponding
  11. //                    Hash table for copyright reasons.
  12.  
  13. // Written by Michael Hopkins, and Levi Brown, 1.0, June 27, 1997.
  14.  
  15. /**
  16.  * Many useful color related utility functions for color manipulation.
  17.  * <p>
  18.  * @version 1.1, August 5, 1997
  19.  * @author  Symantec
  20.  */
  21. public class ColorUtils
  22. {
  23.     /**
  24.      * Do not use, this is an all-static class.
  25.      */
  26.     public ColorUtils()
  27.     {
  28.     }
  29.  
  30.     /**
  31.      * Darkens a given color by the specified percentage.
  32.      * @param r The red component of the color to darken.
  33.      * @param g The green component of the color to darken.
  34.      * @param b The blue component of the color to darken.
  35.      * @param percent percentage to darken.  Needs to be <= 1 && >= 0.
  36.      * @return a new Color with the desired characteristics.
  37.      * @exception IllegalArgumentException
  38.      * if the specified percentage value is unacceptable
  39.      */
  40.     public static Color darken( int r, int g, int b, double percent ) throws IllegalArgumentException
  41.     {
  42.         symantec.itools.util.GeneralUtils.checkValidPercent(percent);
  43.  
  44.         return new Color( Math.max((int)(r * (1-percent)), 0),
  45.                             Math.max((int)(g * (1-percent)),0),
  46.                             Math.max((int)(b * (1-percent)),0));
  47.     }
  48.  
  49.     /**
  50.      * Darkens a given color by the specified percentage.
  51.      * @param to the Color to darken.
  52.      * @param percent percentage to darken.  Needs to be <= 1 && >= 0.
  53.      * @return a new Color with the desired characteristics.
  54.      * @exception IllegalArgumentException
  55.      * if the specified percentage value is unacceptable
  56.      */
  57.     public static Color darken( Color c, double percent ) throws IllegalArgumentException
  58.     {
  59.         symantec.itools.util.GeneralUtils.checkValidPercent(percent);
  60.  
  61.         int r, g, b;
  62.         r = c.getRed();
  63.         g = c.getGreen();
  64.         b = c.getBlue();
  65.         return darken( r, g, b, percent );
  66.     }
  67.  
  68.     /**
  69.      * Lightens a given color by the specified percentage.
  70.      * @param r The red component of the color to lighten.
  71.      * @param g The green component of the color to lighten.
  72.      * @param b The blue component of the color to lighten.
  73.      * @param percent percentage to lighten.  Needs to be <= 1 && >= 0.
  74.      * @return a new Color with the desired characteristics.
  75.      * @exception IllegalArgumentException
  76.      * if the specified percentage value is unacceptable
  77.      */
  78.     public static Color lighten( int r, int g, int b, double percent ) throws IllegalArgumentException
  79.     {
  80.         symantec.itools.util.GeneralUtils.checkValidPercent(percent);
  81.  
  82.         int r2, g2, b2;
  83.         r2 = r + (int)((255 - r) * percent );
  84.         g2 = g + (int)((255 - g) * percent );
  85.         b2 = b + (int)((255 - b) * percent );
  86.         return new Color( r2, g2, b2 );
  87.     }
  88.  
  89.     /**
  90.      * Lightens a given color by the specified percentage.
  91.      * @param to the Color to lighten.
  92.      * @param percent percentage to lighten.  Needs to be <= 1 && >= 0.
  93.      * @return a new Color with the desired characteristics.
  94.      * @exception IllegalArgumentException
  95.      * if the specified percentage value is unacceptable
  96.      */
  97.     public static Color lighten( Color c, double percent ) throws IllegalArgumentException
  98.     {
  99.         symantec.itools.util.GeneralUtils.checkValidPercent(percent);
  100.  
  101.         int r, g, b;
  102.         r = c.getRed();
  103.         g = c.getGreen();
  104.         b = c.getBlue();
  105.         return lighten( r, g, b, percent );
  106.     }
  107.  
  108.     /**
  109.      * Fades from one color to another by the given percentage.
  110.      * @param from the Color to fade from.
  111.      * @param to the Color to fade to.
  112.      * @param percent percentage to fade.  Needs to be <= 1 && >= 0.
  113.      * @return a new Color with the desired characteristics.
  114.      * @exception IllegalArgumentException
  115.      * if the specified percentage value is unacceptable
  116.      */
  117.     public static Color fade( Color from, Color to, double percent ) throws IllegalArgumentException
  118.     {
  119.         symantec.itools.util.GeneralUtils.checkValidPercent(percent);
  120.  
  121.         int from_r, from_g, from_b;
  122.         int to_r, to_g, to_b;
  123.         int r, g, b;
  124.  
  125.         from_r = from.getRed();
  126.         from_g = from.getGreen();
  127.         from_b = from.getBlue();
  128.  
  129.         to_r = to.getRed();
  130.         to_g = to.getGreen();
  131.         to_b = to.getBlue();
  132.  
  133.         if (from_r > to_r)
  134.             r = to_r + (int)((from_r - to_r)* (1 - percent));
  135.         else
  136.             r = to_r - (int)((to_r - from_r)* (1 - percent));
  137.         if (from_g > to_r)
  138.             g = to_g + (int)((from_g - to_g)* (1 - percent));
  139.         else
  140.             g = to_g - (int)((to_g - from_g)* (1 - percent));
  141.         if (from_b > to_b)
  142.             b = to_b + (int)((from_b - to_b)* (1 - percent));
  143.         else
  144.             b = to_b - (int)((to_b - from_b)* (1 - percent));
  145.  
  146.         return new Color(r, g, b);
  147.     }
  148.  
  149.     /**
  150.      * Given a Color this function determines the lightness percent.
  151.      * @param c The Color to calculate from.  If null, it will return 0.
  152.      * @return the percent light of the specified color.  This value will be
  153.      * >= 0 && <= 1.
  154.      */
  155.     public static double lightness(Color c)
  156.     {
  157.         if(c == null)
  158.             return 0;
  159.  
  160.         double r, g, b, max, min;
  161.         r = c.getRed();
  162.         g = c.getGreen();
  163.         b = c.getBlue();
  164.         max = (Math.max(r, Math.max(g, b)) / 255) / 2;
  165.         min = (Math.min(r, Math.min(g, b)) / 255) / 2;
  166.         return (max + min);
  167.     }
  168.  
  169.     /**
  170.      * Used to calculate a hilight color from a given color.
  171.      * @param c The color to use in the calculation.  If null, then
  172.      * it will return null.
  173.      * @return the newly calculated hilight color.
  174.      */
  175.     public static Color calculateHilightColor(Color c)
  176.     {
  177.         if(c == null)
  178.             return null;
  179.  
  180.         double lightness = lightness(c);
  181.  
  182.         if (lightness >= 0.90)
  183.         {
  184.             return(symantec.itools.awt.util.ColorUtils.darken(c, 0.100));
  185.         }
  186.         else if (lightness <= 0.20)
  187.         {
  188.             return(symantec.itools.awt.util.ColorUtils.lighten(c, 0.600));
  189.         }
  190.         else
  191.         {
  192.             return(symantec.itools.awt.util.ColorUtils.lighten(c, 0.600));
  193.         }
  194.     }
  195.  
  196.     /**
  197.      * Used to calculate a shadow color from a given color.
  198.      * @param c The color to use in the calculation  If null, then
  199.      * it will return null.
  200.      * @return the newly calculated shadow color.
  201.      */
  202.     public static Color calculateShadowColor(Color c)
  203.     {
  204.         if(c == null)
  205.             return null;
  206.  
  207.         double lightness = lightness(c);
  208.  
  209.         if (lightness >= 0.90)
  210.         {
  211.             return(symantec.itools.awt.util.ColorUtils.darken(c, 0.250));
  212.         }
  213.         else if (lightness <= 0.20)
  214.         {
  215.             return(symantec.itools.awt.util.ColorUtils.lighten(c, 0.200));
  216.         }
  217.         else
  218.         {
  219.             return(symantec.itools.awt.util.ColorUtils.darken(c, 0.250));
  220.         }
  221.     }
  222. }
  223.